﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace 学生选课及成绩查询管理系统
{
    public partial class updateStudentInfo : Form
    {
        public updateStudentInfo()
        {
            InitializeComponent();
        }

        private void btn_sub_Click(object sender, EventArgs e)
        {
            string stuno = txt_no.Text;
            string sname = txt_name.Text;
            string sex = txt_sex.Text;
            string age = txt_age.Text;
            string clano = txt_claNo.Text;
            if (string.IsNullOrEmpty(sname) || string.IsNullOrEmpty(sex) || string.IsNullOrEmpty(age) || string.IsNullOrEmpty(clano))
            {
                MessageBox.Show("信息输入不完整");
                return;
            }
            int flag = -1;
            if (int.TryParse(sex, out flag))
            {
                if (flag == 0 || flag == 1)
                {

                }
                else
                {
                    MessageBox.Show("请输入0或1代表性别！");
                    return;
                }
            }
            else
            {
                MessageBox.Show("请输入0或1代表性别！");
                return;
            }
            if (int.TryParse(age, out flag))
            {
                if (flag < 0 || flag > 150)
                {
                    MessageBox.Show("请输入0-150之间的整型数值！");
                    return;
                }
            }
            else
            {
                MessageBox.Show("请输入0-150之间的整型数值！");
                return;
            }
            string sql = "update dbo.student set stuname=@sname,stugender=@sex,stuage=@age,classno=@clano where stuno=@stuno";
            SqlParameter[] arraysql = { new SqlParameter("@sname", sname), new SqlParameter("@sex", sex),new SqlParameter("@age",sex),
                                      new SqlParameter("@clano",clano),new SqlParameter("@stuno",stuno)};
            int res = DBAccess.GetState(sql, arraysql);
            if (res != 0)
            {
                MessageBox.Show("修改该学生信息成功"); 
                this.Dispose();
            }
            else
            {
                MessageBox.Show("修改失败");
            }
        }

        private void txt_name_Click(object sender, EventArgs e)
        {
            string stuno = txt_no.Text;
            if (stuno == "")
            {
                return;
            }
            string sql = "select count(*) from dbo.student where stuno=@stuno";
            SqlParameter[] arraysql = { new SqlParameter("@stuno", stuno) };

            object o = DBAccess.GetOne(sql, arraysql);
            if ((int)o == 1)
            {
                txt_tishi.Text = "✓";
                txt_tishi.ForeColor = Color.Green;
            }
            else
            {
                MessageBox.Show("请输入正确的学号");
                txt_no.Focus();
                return;
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
